package com.liangzhong.modules.PlayerCenter.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.events.PlayerCenterEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.proxyconstant.PlayerCenter.PlayerCenterConstant;
	import com.lz.football.module.base.IModuleView;
	import com.lz.football.module.playerCenter.IPlayerCenter;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	import lz.controls.Alert;
	
	import org.puremvc.as3.interfaces.INotification;
	
	public class PlayerCenterMediator extends ModuleMediator
	{
		public static const MediatorName :String = "PlayerCenterMediator";
		
		/**
		 *打开界面的类型 
		 */
		private var _obj:Object;
		public function PlayerCenterMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get view():IPlayerCenter
		{
			return viewComponent as IPlayerCenter;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			AppEventDispatcher.getInstance().addEventListener(PlayerCenterEvent.REMOVE_MODEL,onremoveMode);
			AppEventDispatcher.getInstance().addEventListener(PlayerCenterEvent.UPGRADE_PLAYERCENTER,onUpgradeHandler);
			AppEventDispatcher.getInstance().addEventListener(PlayerCenterEvent.CLEAR_UP_TIME,onClearTimeHandler);
			AppEventDispatcher.getInstance().addEventListener(PlayerCenterEvent.REPLY_PLAYER_POWER,onReplayPowerHandler);
			AppEventDispatcher.getInstance().addEventListener(PlayerCenterEvent.REPLY_BUFF,onReplyBuffHandler);
			
			AppEventDispatcher.getInstance().addEventListener(PlayerCenterEvent.REMOVE_PLAYER,onremovePlayerHandler);
			
			AppEventDispatcher.getInstance().addEventListener(PlayerCenterEvent.ADD_PLAYER_CELL,onAddPlayerCellHandler);
			AppEventDispatcher.getInstance().addEventListener(PlayerCenterEvent.SAVE_BATTLEARRAY,onSaveBATTLEARRAYHandler);
			AppEventDispatcher.getInstance().addEventListener(PlayerCenterEvent.REPLACE_EQUIP,onReplaceEquipHandler);
			AppEventDispatcher.getInstance().addEventListener(PlayerCenterEvent.CHANGE_PLAYER_NUMBER,onChangePlayerNumBerHandler);
		}
		
		protected function onChangePlayerNumBerHandler(event:PlayerCenterEvent):void
		{
			this.sendNotification(PlayerCenterConstant.PLAYERCENTER_MEDIATOR_NOTIFICATION,
				event.param,PlayerCenterConstant.CHANGE_PLAYER_NUMBER);
		}
		
		protected function onReplaceEquipHandler(event:PlayerCenterEvent):void
		{
			this.sendNotification(PlayerCenterConstant.PLAYERCENTER_MEDIATOR_NOTIFICATION,
				event.param,PlayerCenterConstant.REPLACE_EQUIP);
		}
		
		/**
		 *保存阵容 
		 * @param event
		 * 
		 */
		protected function onSaveBATTLEARRAYHandler(event:PlayerCenterEvent):void
		{
			this.sendNotification(PlayerCenterConstant.PLAYERCENTER_MEDIATOR_NOTIFICATION,
				event.param,PlayerCenterConstant.SAVE_BATTLEARRAY);
		}
		
		protected function onAddPlayerCellHandler(event:PlayerCenterEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(PlayerCenterConstant.PLAYERCENTER_MEDIATOR_NOTIFICATION,
				event.param,PlayerCenterConstant.ADD_PLAYER_CELL);
		}
		
		/**
		 * 删除球员 
		 * @param event
		 * 
		 */
		protected function onremovePlayerHandler(event:PlayerCenterEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(PlayerCenterConstant.PLAYERCENTER_MEDIATOR_NOTIFICATION,
				event.param,PlayerCenterConstant.REMOVE_PLAYER);
		}
		
		
		
		protected function onReplyBuffHandler(event:PlayerCenterEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(PlayerCenterConstant.PLAYERCENTER_MEDIATOR_NOTIFICATION,
				event.param,PlayerCenterConstant.REPLY_BUFF);
		}
		
		protected function onReplayPowerHandler(event:PlayerCenterEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(PlayerCenterConstant.PLAYERCENTER_MEDIATOR_NOTIFICATION,
				event.param,PlayerCenterConstant.REPLY_POWER);
		}
		
		/**
		 * 清除球员中心冷却时间 
		 * @param event
		 * 
		 */
		protected function onClearTimeHandler(event:PlayerCenterEvent):void
		{
			this.sendNotification(PlayerCenterConstant.PLAYERCENTER_MEDIATOR_NOTIFICATION,
				event.param,PlayerCenterConstant.CLEAR_UP_TIME);
		}
		
		/**
		 *升级球员中心 
		 * @param event
		 * 
		 */
		protected function onUpgradeHandler(event:PlayerCenterEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(PlayerCenterConstant.PLAYERCENTER_MEDIATOR_NOTIFICATION,
				event.param,PlayerCenterConstant.PLAYERCENTER_UPGRADE);
		}
		
		protected function onremoveMode(event:Event):void
		{
			// TODO Auto-generated method stub
			facade.removeMediator(MediatorName);
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			AppEventDispatcher.getInstance().removeEventListener(PlayerCenterEvent.REMOVE_MODEL,onremoveMode);
			AppEventDispatcher.getInstance().removeEventListener(PlayerCenterEvent.UPGRADE_PLAYERCENTER,onUpgradeHandler);
			AppEventDispatcher.getInstance().removeEventListener(PlayerCenterEvent.CLEAR_UP_TIME,onClearTimeHandler);
			AppEventDispatcher.getInstance().removeEventListener(PlayerCenterEvent.REPLY_PLAYER_POWER,onReplayPowerHandler);
			AppEventDispatcher.getInstance().removeEventListener(PlayerCenterEvent.REPLY_BUFF,onReplyBuffHandler);
			
			AppEventDispatcher.getInstance().removeEventListener(PlayerCenterEvent.REMOVE_PLAYER,onremovePlayerHandler);
			
			AppEventDispatcher.getInstance().removeEventListener(PlayerCenterEvent.ADD_PLAYER_CELL,onAddPlayerCellHandler);
			AppEventDispatcher.getInstance().removeEventListener(PlayerCenterEvent.SAVE_BATTLEARRAY,onSaveBATTLEARRAYHandler);
			AppEventDispatcher.getInstance().removeEventListener(PlayerCenterEvent.REPLACE_EQUIP,onReplaceEquipHandler);
			AppEventDispatcher.getInstance().removeEventListener(PlayerCenterEvent.CHANGE_PLAYER_NUMBER,onChangePlayerNumBerHandler);
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [PlayerCenterConstant.GET_PLAYERCENTER_UPGRADE,
				PlayerCenterConstant.GET_CLEAR_UP_TIME,
				PlayerCenterConstant.GET_REMOVE_PLAYER,
				PlayerCenterConstant.GET_ADD_PLAYER_CELL,
				PlayerCenterConstant.GET_CHANGE_POWERANDBUFF,
				PlayerCenterConstant.GET_SAVE_BATTLEARRAY,
				PlayerCenterConstant.GET_REPLACE_EQUIP,
				PlayerCenterConstant.GET_CHANGE_PLAYER_NUMBER,
				PlayerCenterConstant.GET_ADD_PLAYER,
				NotificationConstants.COMMON_DROP_TIME].concat(super.listNotificationInterests());
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case PlayerCenterConstant.GET_PLAYERCENTER_UPGRADE://返回球员中心升级
//					view.playerCenterUpGrade(notification.getBody());
					break;
				case NotificationConstants.COMMON_DROP_TIME://返回清除冷却时间
					if(notification.getBody() == BuildingType.PLAYERCENTER)
					{
						AppData.getInstance().playerCenterDataManager.playerCenterVo.pretime = 0;
//						view.clearUpTime(notification.getBody());
						Alert.showErrorTip("冷却成功");
					}
					
					break;

				case PlayerCenterConstant.GET_REMOVE_PLAYER:
					view.removePlayer(notification.getBody());
					break;
				case PlayerCenterConstant.GET_ADD_PLAYER_CELL:
					view.addPlayerCell(notification.getBody());
					break;
				case PlayerCenterConstant.GET_CHANGE_POWERANDBUFF:
					if(view != null)
						view.changePowerAndBuff(notification.getBody());
					break;
				case PlayerCenterConstant.GET_SAVE_BATTLEARRAY://保存阵容
					view.save_battlearray(notification.getBody());
					break;
				case PlayerCenterConstant.GET_REPLACE_EQUIP:
					view.replace_equip(notification.getBody());
					break;
				case PlayerCenterConstant.GET_CHANGE_PLAYER_NUMBER://改变号码
					view.changePlayerNumber(notification.getBody());
					break;
				case PlayerCenterConstant.GET_ADD_PLAYER:
					view.addPlayer(notification.getBody());
					break;
				
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			if(AppData.getInstance().playerCenterDataManager.playerCenterVo != null)
			{
//				AppData.getInstance().playerCenterDataManager.updataPlayerphysical();
				
				this.sendNotification(PlayerCenterConstant.PLAYERCENTER_MEDIATOR_NOTIFICATION,
					null,PlayerCenterConstant.UPDATE_PLAYER_POWER);
			}
			resName = ResourceConst.PLAYERCENTER_SWF;
			if(ResourceManager.instance.isContainResource(ResourceConst.PLAYERCENTER_SWF))
			{
				showUI();
				//@lake 2013-11-20 guide use
				AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MODULE_LOAD_RES_COMPLETE);
			}
			else
			{
				loader = new QueueLoader();
				var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).playerCenter[0];
				bytesTotal = int(clubConfig.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in clubConfig.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			
			showUI();
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.PLAYERCENTER_SWF,resList)
		}
		/**
		 * 
		 * 
		 */
		private function showUI():void
		{
			var cView :IModuleView = Reflection.createInstance("com.lz.football.module.playerCenter.PlayerCenterView");
			cView.initUI();
			viewComponent = cView;
			GlobalDef.GameWindowContainer.addChild(viewComponent as DisplayObject);
			view.showViewType(this.param);
			addEventListeners();
			//设置缓存位图
			setCacheAsBitmap(true);
		}
		/**
		 *显示面板 {type:2}
		 * 2打开阵容设置面板
		 * @param $type
		 * 
		 */
		public function showViewType($obj:Object):void
		{
			if(view != null)view.showViewType($obj);
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			(viewComponent as IModuleView).destroyView();
			viewComponent = null;
		}
	}
}